
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch

# 指定本地模型路径
model_path = "/home/fangning/work/LLM/models/deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B"

# 加载tokenizer和模型
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
    model_path,
    trust_remote_code=True,
    torch_dtype=torch.float16,
    device_map="auto"
)

# 构造对话格式输入
input_text = "你好"
messages = [{"role": "user", "content": input_text}]

# 应用对话模板并生成
inputs = tokenizer.apply_chat_template(
    messages,
    add_generation_prompt=True,
    return_tensors="pt"
).to(model.device)

outputs = model.generate(
    inputs,
    max_length=512,
    do_sample=True,
    temperature=0.7,
    top_p=0.9
)

# 解码时跳过特殊token
print(tokenizer.decode(outputs[0][inputs.shape[1]:], skip_special_tokens=True))
